package com.starbaba.stat.view;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.time.Duration;
import java.time.LocalDate;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;

import com.base.common.log.LogService;
import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.common.HttpConnection;
import com.starbaba.core.utils.HttpClient;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.utils.JSONUtils;
import com.starbaba.redis.JedisUtils;
import com.starbaba.stat.service.StatCommActivityRegisterService;
import com.starbaba.stat.service.impl.StatCommActivityRegisterServiceImpl;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;
import com.tuanzi.server.loan_base.model.db.stat.StatCommActivityRegister;
import com.tuanzi.server.loan_base.util.DateUtils;

/**
 * 推广活动用户注册统计Action
 * @author chenweijun
 *
 */
@SuppressWarnings({"unchecked","rawtypes","serial"})
public class StatCommActivityRegisterAction extends BaseAction implements ModelDriven<StatCommActivityRegister>{
	
	private StatCommActivityRegister model;
	private StatCommActivityRegisterService service;
	
	private InputStream inputStream;   //导出文件输入流
	private String fileName;   //导出文件名

	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public StatCommActivityRegisterAction(){
		this.service = new StatCommActivityRegisterServiceImpl();
	}
	
	@Override
	public StatCommActivityRegister getModel() {
		if(model == null){
			model = new StatCommActivityRegister();
		}
		return model;
	}
	
	/**
	 * 查询列表
	 */
	public void getList(){
		
		parsePageRequest();
		PageResponse pageResponse=new PageResponse();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		List list=service.getList(paramsMap, pageRequest.getIDisplayStart(),pageRequest.getIDisplayLength());
		int count=service.countList(paramsMap);
		try {
			
			pageResponse.setITotalDisplayRecords(count);
			pageResponse.setITotalRecords(count);
			pageResponse.setAaData(list);
			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
			
		} catch(IOException e) {
			LogService.error(e);
		}
		
	}
	
	
	/**
	 * 导出列表
	 */
	public String exportList(){
		
		parsePageRequest();
		paramsMap=PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		
		String result = service.exportList(paramsMap);
		  try {
			  inputStream = new ByteArrayInputStream(result.getBytes("GBK"));
			  fileName = "comm_activity_register" + System.currentTimeMillis() + ".csv";
		} catch (UnsupportedEncodingException e) {
			LogService.error(e);
			return ERROR;
		}
		return SUCCESS;
		
	}
	
	/**
	 * 即时统计数据
	 * @return
	 */
	public String instantStatistics(){
		
		//限制每五分钟跑一次
		if("1".equals(JedisUtils.get(LoanRedisKey.STAT_COMM_ACTIVITY_REGISTER_SWITCH))) {
			return SUCCESS;
		}


		JedisUtils.setex(LoanRedisKey.STAT_COMM_ACTIVITY_REGISTER_SWITCH, (int)Duration.ofMinutes(5).getSeconds(), "1");
		//统计当天的数据
		String cdate = LocalDate.now().toString();
		String url = "http://dkzj.moneyfanli.com/loan_timer/time?funid=24&time=" + cdate;
		String response = HttpConnection.connection(url, HttpConnection.GET);
		LogService.info("即时统计通用活动结果： " + response);
		JSONObject responseJson = JSONObject.fromObject(response);
		JSONObject resultJson = responseJson.getJSONObject("result");
		Integer code = resultJson.getInt("status");
		if(code == 1){
			JedisUtils.del(LoanRedisKey.STAT_COMM_ACTIVITY_REGISTER_SWITCH);
			return SUCCESS;
		}else{
			return ERROR;
		}
		
		
	}

}
